package edu.ustb.mis.dm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import edu.ustb.mis.dm.Constants;

public class Utils {
	@SuppressWarnings("unchecked")
	public static <T>T deserialization(final File file) {
		T t = null;
		try {
			final ObjectInputStream input = new ObjectInputStream(new FileInputStream(file.getPath()));
			try {
				t = (T)input.readObject();
			} catch (final ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	// 反串行化
	public static <T>T deserialization(final String path) {
		final File file = new File(path);
		if (!file.exists()) {
			return null;
		}
		return deserialization(file);
	}

	public static File getFile(final String pathname) {
		return new File(pathname);
	}

	/**
	 * 从reader读取一行数据的简单封装
	 * 
	 * @param reader
	 * @return
	 * @throws IOException
	 */
	public static String readLine(final Reader reader) throws IOException {
		return ((BufferedReader)reader).readLine();
	}

	public static Set<String> readStopWords() {
		final Set<String> set = new HashSet<String>();
		try {
			final Reader reader = new BufferedReader(new FileReader(Constants.STOP_WORDS_PATH));
			String word = ((BufferedReader)reader).readLine();
			while (word != null) {
				set.add(word);
				word = Utils.readLine(reader);
			}
		} catch (final Exception e) {
			System.err.println("获取stopwords列表失败");;
			e.printStackTrace();
		}
		return set;
	}

	public static void serialization(final Object o, final String serialization_path) {
		final File dist = new File(serialization_path);
		if (dist.exists()) {
			dist.delete();
		}
		try {
			dist.createNewFile();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		try {
			final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dist));
			out.writeObject(o);
		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 字符串分割方法
	 * 
	 * @param str
	 * @param separatorChar
	 * @return
	 */
	public static String[] split(final String str, final char separatorChar) {
		final int len = str.length();
		final List<String> list = new ArrayList<String>();
		int i = 0, start = 0;
		boolean match = false;
		while (i < len) {
			if (str.charAt(i) == separatorChar) {
				if (match) {
					list.add(str.substring(start, i));
					match = false;
				}
				start = ++i;
				continue;
			}
			match = true;
			i++;
		}
		return list.toArray(new String[list.size()]);
	}
}
